home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / VECTORS.PAK / IVECTOR.CPP next >
C/C++ Source or Header  |  1997-05-06  |  1KB  |  59 lines

  1. // ---------------------------------------------------------------------------
  2. // Copyright (C) 1994 Borland International
  3. // ivector.cpp
  4. //    Must link with myclass.cpp.
  5. // ---------------------------------------------------------------------------
  6.  
  7. #include <classlib/vectimp.h>
  8. #include <classlib/alloctr.h>
  9. #include <iostream.h>
  10. #include <strstrea.h>
  11. #include "../myclass.h"
  12.  
  13. typedef TMICVectorImp<MyClass, TStandardAllocator> ContainerType;
  14. typedef TMICVectorIteratorImp<MyClass, TStandardAllocator> IteratorType;
  15.  
  16. const int MaxItems=6;
  17.  
  18. void ForEachCallBack(MyClass& mc, void* s)
  19. {
  20.     cout << (char*)s << mc << endl;
  21. }
  22.  
  23. void AddItems(ContainerType& container, int numItems)
  24. {
  25.     for( int i=numItems; i>0; i-- )
  26.         {
  27.         char buf[80];
  28.         ostrstream str( buf, sizeof(buf) );
  29.         str << i << " hello" << ends;
  30.         container.Add( new MyClass(buf) );
  31.         }
  32. }
  33.  
  34. void UseForwardIterator(ContainerType& container)
  35. {
  36.     IteratorType iterator(container);
  37.     while( iterator )
  38.         {
  39.         cout << *iterator.Current() << endl;
  40.         iterator++;
  41.         }
  42. }
  43.  
  44. int main()
  45. {
  46.     ContainerType container(MaxItems);
  47.     AddItems(container, MaxItems);
  48.  
  49.     cout << "--- Starting ForEach" << endl;
  50.     container.ForEach(ForEachCallBack, (void*)"FE ");
  51.  
  52.     cout << "--- Starting Iterator (forward)" << endl;
  53.     UseForwardIterator(container);
  54.  
  55.     container.Flush(1);
  56.  
  57.     return 0;
  58. }
  59.